package dao.mysql;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

import model.LeaveDTO;
import model.LeaveTypeDTO;
import exception.DAOSystemException;
import dao.LeaveTypeDAO;

public class LeaveTypeDAOImpl implements LeaveTypeDAO {
	MySqlImpl mysqlImpl = new MySqlImpl();

	@Override
	public LeaveTypeDTO findLeaveType(String id) throws DAOSystemException {
		// TODO Auto-generated method stub
		String selectSql = "SELECT * FROM leavetype WHERE id='" + id.trim()
		+  "' or type='" + id.trim()+"';";
		LeaveTypeDTO oneLeave = new LeaveTypeDTO();
		try {
			ResultSet rs = mysqlImpl.get(selectSql);

			String leaveTypeId = null;
			while (rs.next()) {
				oneLeave.setId(rs.getString("id"));
				oneLeave.setType(rs.getString("type"));
			}

		} catch (Exception e) {
			String error = "Error selecting leave type. Nested Exception is: "
					+ e;
			Logger.getLogger(getClass().getName()).log(Level.SEVERE, error);
			throw new DAOSystemException(error);
		} finally {
			try {
				mysqlImpl.close();
			} catch (Exception e) {
			}
		}

		return oneLeave;

	}

	@Override
	public ArrayList<LeaveTypeDTO> findAllLeaveTypes()
			throws DAOSystemException {
		// TODO Auto-generated method stub
		ArrayList<LeaveTypeDTO> items = new ArrayList<LeaveTypeDTO>();
		String selectSql = "SELECT * FROM leavetype;";

		try {

			Logger.getLogger(getClass().getName()).log(Level.INFO,
					"Executing select: " + selectSql);
			ResultSet rs = mysqlImpl.get(selectSql);
			LeaveTypeDTO oneLeave = null;
			String leaveTypeId = null;
			while (rs.next()) {
				oneLeave = new LeaveTypeDTO();
				oneLeave.setId(rs.getString("id"));
				oneLeave.setType(rs.getString("type"));

				items.add(oneLeave);
			}

		} catch (Exception e) {
			String error = "Error selecting leave type. Nested Exception is: "
					+ e;
			Logger.getLogger(getClass().getName()).log(Level.SEVERE, error);
			throw new DAOSystemException(error);
		} finally {
			try {
				mysqlImpl.close();
			} catch (Exception e) {
			}
		}
		return items;
	}

	@Override
	public void insertLeaveType(LeaveTypeDTO leaveType)
			throws DAOSystemException {
		// TODO Auto-generated method stub
		String insertSql = "INSERT INTO leavetype VALUES('" + leaveType.getId()
				+ "', '" + leaveType.getType() + "');";

		try {

			Logger.getLogger(getClass().getName()).log(Level.INFO,
					"Executing insert: " + insertSql);
			int res = mysqlImpl.execute(insertSql);

		} catch (Exception e) {
			String error = "Error selecting leave type. Nested Exception is: "
					+ e;
			Logger.getLogger(getClass().getName()).log(Level.SEVERE, error);
			throw new DAOSystemException(error);
		} finally {
			try {
				mysqlImpl.close();
			} catch (Exception e) {
			}
		}

	}

	@Override
	public void updateLeaveType(LeaveTypeDTO leaveType)
			throws DAOSystemException {
		// TODO Auto-generated method stub
		String updateSql = "UPDATE leavetype SET type = '" + leaveType.getType()
		
		+ "' WHERE id='"
		+ leaveType.getId() + "';";

		try {

			Logger.getLogger(getClass().getName()).log(Level.INFO,
					"Executing update: " + updateSql);
			int res = mysqlImpl.execute(updateSql);

		} catch (Exception e) {
			String error = "Error selecting leave type. Nested Exception is: " + e;
			Logger.getLogger(getClass().getName()).log(Level.SEVERE, error);
			throw new DAOSystemException(error);
		} finally {
			try {
				mysqlImpl.close();
			} catch (Exception e) {
			}
		}

	}

	@Override
	public void deleteLeaveType(LeaveTypeDTO leaveType)
			throws DAOSystemException {
		// TODO Auto-generated method stub
		String deleteSql = "DELETE FROM leavetype WHERE id='"
			+ leaveType.getId().trim() + "';";

	try {
		mysqlImpl.execute(deleteSql);
	} catch (SQLException e) {
		String error = "Error deleting leave type. Nested Exception is: " + e;
		Logger.getLogger(getClass().getName()).log(Level.SEVERE, error);
		try {
			mysqlImpl.rollBack();
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		throw new DAOSystemException(error);
	} finally {
		try {
			mysqlImpl.close();
		} catch (Exception e) {
		}
	}

}


	}


